<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.mousewheel.js"></script>
    <title>聊天机器人</title>
</head>

<body>
    <div class="wrap">
        <!-- 头部 Header 区域 -->
        <div class="header">
            <h3>小思同学</h3>
            <img src="img/person01.png" alt="icon" />
        </div>
        <!-- 中间 聊天内容区域 -->
        <div class="main">
            <ul class="talk_list" style="top: 0px;">
                <!-- <li class="left_word">
                    <img src="img/person01.png" /> <span>你好</span>
                </li>
                <li class="right_word">
                    <img src="img/person02.png" /> <span>你好哦</span>
                </li> -->
            </ul>
            <div class="drag_bar" style="display: none;">
                <div class="drager ui-draggable ui-draggable-handle" style="display: none; height: 412.628px;"></div>
            </div>
        </div>
        <!-- 底部 消息编辑区域 -->
        <div class="footer">
            <img src="img/person02.png" alt="icon" />
            <input type="text" placeholder="说的什么吧..." class="input_txt" />
            <input type="button" value="发 送" class="input_sub" />
        </div>
    </div>
    <!-- 语音播放标签 -->
    <audio src="" id="voice" autoplay style="display: none;"></audio>
    <script type="text/javascript" src="js/scroll.js"></script>
    <script>
        $(function() {
            //1.给发送按钮注册点击事件
            $('.input_sub').on('click', function() {
                // console.log(1);
                //2.获取输入框里的内容
                // console.log($('.input_txt').val());
                var input_txt = $('.input_txt').val().trim();
                //这里的trim是原生js里的方法
                //3.展示在聊天框中
                var rtli = `
          <li class="right_word">
              <img src="img/person02.png" /> <span>${input_txt}</span>
          </li>
          `;
                $('.talk_list').append(rtli);
                //4.清空输入框的内容
                $('.input_txt').val('');
                // 5.将获取到的信息向服务器发送数据
                $.get(
                    'http://www.liulongbin.top:3006/api/robot', {
                        spoken: input_txt,
                    },
                    function(res) {
                        // console.log(res);
                        //获取到左侧要回复的信息
                        var resmag = res.data.info.text;
                        console.log(resmag);
                        //添加到聊天栏
                        var leli = `
                    <li class="left_word">
                         <img src="img/person01.png" /> <span>${resmag}</span>
                    </li>
                    `;
                        $('.talk_list').append(leli);
                        //添加语音播报
                        $.get(
                            'http://www.liulongbin.top:3006/api/synthesize', {
                                text: resmag,
                            },
                            function(res) {
                                //获取res里的voiceUrl地址 并将radio的src属性修改成该地址
                                var voiceData = res.voiceUrl;
                                $('#voice').attr('src', voiceData);
                            }
                        );
                    }
                );
            });
            $('.input_txt').on('keyup', function(e) {
                // console.log(1);
                // console.log(e.keyCode);
                if (e.keyCode === 13) {
                    // $('.input_sub').click();
                    //触发所有的绑定事件
                    $('.input_sub').trigger('click');
                }
            });
            // 初始化右侧滚动条
            // 这个方法定义在scroll.js中
            resetui();
        });
    </script>
</body>

</html>